package com.xyhuo.leetcode.january;

import com.xyhuo.leetcode.common.TreeNode;

/**
 * 路径总和
 * https://leetcode.cn/problems/path-sum/
 *
 * @time 2024/1/10
 */
public class HasPathSum {

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        search(root, targetSum, 0);
        return ans;
    }

    private boolean ans = false;

    private void search(TreeNode root, int targetSum, int sum) {
        if (root.left == null && root.right == null) {
            if (sum + root.val == targetSum) {
                ans = true;
            }
        }
        if (root.left != null) {
            search(root.left, targetSum, sum + root.val);
        }
        if (root.right != null) {
            search(root.right, targetSum, sum + root.val);
        }
    }
}
